<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
          <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible=true">添加分类</el-button>
      </div>
      <!-- 表格区域 -->
        <el-table
      :data="cateList"
      border
      stripe
      style="width: 100%">
      <el-table-column
        type="index"
        label="序号"
        width="50">
      </el-table-column>
      <el-table-column
        prop="cate_name"
        label="分类名称"
        >
      </el-table-column>
      <el-table-column
        prop="cate_alias"
        label="分类别名">
      </el-table-column>
      <el-table-column
        label="操作">
        <template v-slot="{ row }">
          <el-button type="primary" size="mini" @click="showEditDialog(row)">修改</el-button>
          <el-button type="danger" size="mini" @click="hDel(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    </el-card>

    <el-dialog
  title="添加文字分类"
  :visible.sync="addVisible"
  :close-on-click-modal = 'false'
  @closed='$refs.addRef.resetFields()'
  width="35%"
  >
  <!-- 添加的表单 -->
  <el-form :model="addForm" :rules="addRules" ref="addRef" label-width="70px">
    <el-form-item label="分类名称" prop="cate_name">
      <el-input v-model="addForm.cate_name" minlength="1" maxlength="10"></el-input>
    </el-form-item>
    <el-form-item label="分类别名" prop="cate_alias">
      <el-input v-model="addForm.cate_alias" minlength="1" maxlength="15"></el-input>
    </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="addVisible = false">取 消</el-button>
    <el-button type="primary" @click="hSubmit">确 定</el-button>
  </span>
</el-dialog>

    <el-dialog
  title="修改文字分类"
  :visible.sync="editVisible"
  :close-on-click-modal = 'false'
  @closed='$refs.editRef.resetFields()'
  width="35%"
  >
  <!-- 修改的表单 -->
  <el-form :model="editForm" :rules="addRules" ref="editRef" label-width="70px">
    <el-form-item label="分类名称" prop="cate_name">
      <el-input v-model="editForm.cate_name" minlength="1" maxlength="10"></el-input>
    </el-form-item>
    <el-form-item label="分类别名" prop="cate_alias">
      <el-input v-model="editForm.cate_alias" minlength="1" maxlength="15"></el-input>
    </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="editVisible = false">取 消</el-button>
    <el-button type="primary" @click="editSubmit">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data() {
    return {
      cateList: [],
      addVisible: false,
      editVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      editForm: {
        cate_name: '',
        cate_alias: ''
      },
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '请输入1到10位非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$axios.get('/my/cate/list')
      // console.log(res)
      if (res.code !== 0) return this.$message.error('获取文章列表失败')
      // this.$message.success('获取文章列表成功')
      this.cateList = res.data
      // console.log(this.cateList)
    },
    hSubmit() {
      // 1.兜底校验
      this.$refs.addRef.validate(async valid => {
        // 趁早返回
        if (!valid) return
        // 2.发送请求
        const { data: res } = await this.$axios.post('/my/cate/add', this.addForm)
        // 3.提示用户添加成功与否
        if (res.code !== 0) return this.$message.error('发送请求失败')
        this.$message.success('添加成功')
        // 4.关闭弹框
        this.addVisible = false
        // 5.重新获取分类列表
        this.getCateList()
      })
    },
    editSubmit() {
      // 1.兜底校验
      this.$refs.editRef.validate(async valid => {
        // 趁早返回
        if (!valid) return
        // 2.发送请求
        const { data: res } = await this.$axios.put('/my/cate/info', this.editForm)
        // 3.提示用户添加成功与否
        if (res.code !== 0) return this.$message.error('发送请求失败')
        this.$message.success('修改成功')
        // 4.关闭弹框
        this.editVisible = false
        // 5.重新获取分类列表
        this.getCateList()
      })
    },
    showEditDialog(row) {
      if (row.id === 1 || row.id === 2) return this.$message.warning('管理员不允许修改该数据')
      this.editVisible = true
      this.editForm = { ...row }
    },
    hDel(id) {
      if (id === 1 || id === 2) return this.$message.warning('管理员不允许删除')
      this.$confirm('真的要删除吗亲~~', '提示', {
        type: 'warning'
      }).then(async() => {
        // 点击弹框确定调用
        const { data: res } = await this.$axios.delete('/my/cate/del', { params: { id } })
        if (res.code !== 0) return this.$message.error('删除失败')
        this.$message.success(res.message)
        // 删除之后需要再次渲染
        this.getCateList()
      }).catch(() => {}) // 捕获了取消，但是上面都不做
    }
  },
  created() {
    this.getCateList()
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
